﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using WpfNet8Test.Model;
using WpfNet8Test.ZCommand;

namespace WpfNet8Test.ViewModel
{
    internal class MainWindowVM:INotifyPropertyChanged
    {
        //原生的数据绑定方式
        private EmployeeModel _employee;

        public event PropertyChangedEventHandler? PropertyChanged;
        private void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if(handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public EmployeeModel EmployeeM
        {
            get {  return _employee; }
            set 
            { 
                _employee = value;
                RaisePropertyChanged("EmployeeM");
            }
        }
        public MainWindowVM()
        {
            EmployeeM = new EmployeeModel();
            EmployeeM.Name = "小明(绑定)";
            EmployeeM.Age = 44;
            EmployeeM.Profession = "程序员";
        }

        /// <summary>
        /// 命令要执行的方法
        /// </summary>
        void UpdateNameExecute(object sender)
        {
            EmployeeM.Name = "王明（原属性修改）";
            EmployeeM = EmployeeM;//自己赋值自己才能触发nnotice方法
        }

        /// <summary>
        /// 命令是否可以执行
        /// </summary>
        /// <returns></returns>
        bool CanUpdateNameExecute()
        {
            //return false;
            return true;
        }

        /// <summary>
        /// 创建新命令
        /// </summary>
        public ICommand ClickAction
        {
            get
            {
                return new RelayCommand<object>(UpdateNameExecute, CanUpdateNameExecute);
            }
        }

        /// <summary>
        /// 命令要执行的方法
        /// </summary>
        /// <param name="sender"></param>
        void UpdateColorExecute(object sender)
        {
            Button button = sender as Button;
            button.Foreground = Brushes.Red;
        }

        /// <summary>
        /// 命令是否可以执行
        /// </summary>
        /// <returns></returns>
        bool CanUpdateColorExecute()
        {
            return true;
        }

        public ICommand MouseEnterAction
        {
            get
            {
                return new RelayCommand<object>(UpdateColorExecute, CanUpdateColorExecute);
            }
        }

    }
}
